package com.alfayun.fiscalriskanalysis.another.analysis;

import com.alfayun.fiscalriskanalysis.another.indicator.Indicator;
import com.alfayun.fiscalriskanalysis.another.report.Report;
import com.alfayun.fiscalriskanalysis.another.specification.analysis.AnalysisSpec;

import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public final class Analysis {

	private AnalysisSpec spec;

	private List<Indicator> indicators;

	private String id;
	private String name;
	private String orgId;
	private LocalDate startDate;
	private LocalDate endDate;

	private Collection<Report> reports;

	public static Analysis analyse(AnalysisSpec spec, Collection<Report> reports, LocalDate startDate,
			LocalDate endDate) {
		return new Analysis(spec, reports, startDate, endDate);
	}

	public Analysis(AnalysisSpec spec, Collection<Report> reports, LocalDate startDate, LocalDate endDate) {
		this.spec = spec;
		this.startDate = startDate;
		this.endDate = endDate;
		this.reports = reports;
		this.indicators = spec.buildIndicators();

		this.indicators.forEach(it -> it.bindSelector(reports));
		this.indicators.forEach(Indicator::calcMeasure);
		this.indicators.forEach(Indicator::triggerAlert);
	}

}
